/*
 * @Description: 鉴定与处置Context
 * @Author: Zzt
 * @Date: 2022-08-09 14:59:01
 * @LastEditTime: 2022-08-15 11:46:33
 */

import { createContext, useReducer, useEffect, useContext } from 'react';
import { Modal } from 'antd';
import {
  planList,
  deleteAppAndDis,
  createDisposal,
  editAppAndDis
} from '@/api/archives/disposalPlan';
import {
  APPROVAL,
  SET_SELECTED_ITEM,
  ADD_PLAN,
  DELETE_PLAN_ITEM,
  SET_PLAN_LIST
} from '../constants';
import { AppraisalAndDispositionReducer } from '../reducers/AppraisalAndDispositionReducer';

const initialState = {
  plan: [],
  delId: [],
  selectItems: [],
  isVisible: false,
  select: {}
};

const AppraisalAndDispositionContext = createContext();

const AppraisalAndDispositionContextProvider = (props) => {
  const [state, dispatch] = useReducer(AppraisalAndDispositionReducer, initialState);
  const actions = {
    /* 添加计划 */
    addPlan(params) {
      console.log(params);
      return new Promise((resolve, reject) => {
        createDisposal(params)
          .then((res) => {
            const data = res.data;
            console.log(data);
            dispatch({ type: ADD_PLAN, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 获取计划列表 */
    getPlanList(params) {
      return new Promise((resolve, reject) => {
        planList(params)
          .then((res) => {
            const { data } = res;
            console.log(data);
            dispatch({ type: SET_PLAN_LIST, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },
    /**
     * @description: 删除计划
     * @param {number|number[]} id
     * @param {string} title
     */
    deleteAppraisalPlan(id, title) {
      console.log('Fun', id);
      Modal.confirm({
        title: '删除',
        content: title,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteAppAndDis(ids).then((res) => {
            const data = res.data.map((item) => parseInt(item, 10));
            dispatch({ type: DELETE_PLAN_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },
    // 修改
    examineForm(data) {
      return new Promise((resolve, reject) => {
        editAppAndDis(data)
          .then((res) => {
            const { data } = res;
            dispatch({ type: APPROVAL, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    }
  };

  const filters = {
    exportTypeFilter(id) {
      return state.exportTypeList.find((item) => item.id === id)?.name || '-';
    },
    stateFilter(id) {
      return state.stateList.find((item) => item.id === id)?.name || '-';
    },
    stateColorFilter(id) {
      return state.stateList.find((item) => item.id === id)?.color || 'blue';
    }
  };

  useEffect(() => {}, []);
  return (
    <AppraisalAndDispositionContext.Provider value={{ ...state, ...actions, ...filters }}>
      {props.children}
    </AppraisalAndDispositionContext.Provider>
  );
};

export const useAppraisalAndDispositionContext = () => {
  return useContext(AppraisalAndDispositionContext);
};
export { AppraisalAndDispositionContext, AppraisalAndDispositionContextProvider };
